package com.teckteam.ptvsystem.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.teckteam.ptvsystem.model.ResultNode;
import com.teckteam.ptvsystem.model.facade.PTVModel;
import com.teckteam.ptvsystem.view.JourneyPlanner;
import com.teckteam.ptvsystem.view.PTVUserInterface;

/**
 * Listener class for Journey Planner dialog box
 * Interfaces with PTVModel in hopes of displaying results back to user
 *
 */
public class CreateJourneyPlanListener implements ActionListener
{
   private PTVModel model;
   private JourneyPlanner jpDialog;
   
   /**
    * Initialization constructor for Journey Plan data controller
    * 
    * @param dialog
    */
   public CreateJourneyPlanListener(JourneyPlanner dialog)
   {
      this.jpDialog = dialog;
      this.model = PTVUserInterface.getInstance().getApplicationModel();   
   }
   
   public void actionPerformed(ActionEvent e)
   {
      try 
      {
         processForm();
      } catch (JourneyPlanException e1) 
      {
         JOptionPane.showMessageDialog(PTVUserInterface.getInstance(), e1.getMessage());
         e1.printStackTrace();
      }
   }
   
   /**
    * Process the Journey Planner input form; input validation routines performed here.
    * 
    * @throws JourneyPlanException
    */
   private void processForm() throws JourneyPlanException
   {
      String[] vals = jpDialog.getFormValues(),
               temp;
      String
         dTime = vals[0],
         origin = null,
         destination = null,
         sLat = null,
         sLon = null,
         dLat = null,
         dLon = null;
      
      //Form validation
      
      //Source
      //List mode Validation
      if (vals[1].isEmpty())
         throw new JourneyPlanException("Please choose an origin stop!", jpDialog);
      else if (vals[1].charAt(0) == '\1')
      {
         temp = vals[1].split("[:\1]");
         
         //GPS mode input validation
         try
         {
            sLat = temp[1];
            sLon = temp[2];
            origin = temp[3];
            
            int latTest = Integer.parseInt(sLat),
                lonTest = Integer.parseInt(sLon);
                
            if(sLat.isEmpty() || sLon.isEmpty() || latTest < -90 || latTest > 90
                  || lonTest < -180 || lonTest > 180)   
               throw new Exception();
         } catch (Exception ex)
         {
            throw new JourneyPlanException("Your GPS form is invalid.", jpDialog);
         }
         
      } else
         origin = vals[1];
      
      //Destination
      //List mode input validation
      if (vals[2].isEmpty())
         throw new JourneyPlanException("Please choose a destination stop!", jpDialog);
      else if (vals[2].charAt(0) == '\1')
      {
         temp = vals[2].split("[:\1]");
         
         //GPS mode input validation
         try
         {
            dLat = temp[1];
            dLon = temp[2];
            destination = temp[3];
            
            int latTest = Integer.parseInt(sLat),
                lonTest = Integer.parseInt(sLon);
                
            if(dLat.isEmpty() || dLon.isEmpty() || latTest < -90 || latTest > 90
                  || lonTest < -180 || lonTest > 180)   
               throw new Exception();
         } catch (Exception ex)
         {
            throw new JourneyPlanException("Your GPS form is invalid.", jpDialog);
         }
         
      } else
         destination = vals[2];
      
      //Each instance will be ResultNode instances
      //where each node corresponds to relevant departure time information
      ArrayList<ResultNode> stopList = model.getJourneyPlan(origin,
            destination, dTime, sLat, sLon, dLat, dLon);
      
      System.out.println(dTime + origin + destination);
      
      jpDialog.setVisible(false);
      PTVUserInterface.getInstance().showResults(stopList);
   }
   
}
